import axios from "axios";
import { Toast } from "vant";
import Config from "@/config";
import Device from "@/utils/bridge";

interface Result<T, M> {
    code: string | number;
    data: T;
    message: M;
}
class Request {
    private static instance: Request;

    // needLoading = false; // loading框
    caxios: any;

    constructor() {
        this.caxios = axios.create({
            withCredentials: true
        });
        this.caxios.defaults.baseURL = Config.HttpHost;
        this.caxios.defaults.timeout = 8000; // 超时时间
        this.caxios.defaults.headers.post["Content-Type"] = "application/json";

        this.caxios.interceptors.request.use(
            (config: any) => {
                return config;
            },
            (error: any) => {
                return Promise.reject(error);
            }
        );
        this.caxios.interceptors.response.use(
            (response: any) => {
                // TODO 未授权页面返回
                if (response.data.code === "E99008") {
                }
                return response.data;
            },
            () => {
                return {
                    code: "-1",
                    message: "网络异常！"
                };
            }
        );
    }

    public static getInstance() {
        if (!Request.instance) {
            Request.instance = new Request();
        }
        return Request.instance;
    }

    // beforeLoading() {
    //     if (this.needLoading) {
    //         Toast.loading({
    //             message: "加载中...",
    //             forbidClick: true,
    //             duration: 0
    //         });
    //     }
    // }

    // afterLoading() {
    //     if (this.needLoading) {
    //         if (this.needLoading) {
    //             Toast.clear();
    //         }
    //     }
    // }

    // 设置Token
    setHeader(header: object) {
        let headerStr = JSON.stringify(header);
        this.caxios.defaults.headers.common["Auth-Header"] = headerStr;
        Device.SetAutoHeader(header);
    }

    get(url: string, data: any, hideToast?: boolean) {
        return this.caxios.get(url, { params: data }).then((resp: any) => {
            return this.parseRespData(resp, hideToast);
        });
    }

    post(url: string, data: any, hideToast?: boolean) {
        return this.caxios.post(url, data).then((resp: any) => {
            return this.parseRespData(resp, hideToast);
        });
    }

    getWithLoading(url: string, data: any, hideToast?: boolean) {
        Toast.loading({
            message: "加载中...",
            forbidClick: true,
            duration: 0
        });
        return this.caxios.get(url, { params: data }).then((resp: any) => {
            Toast.clear();
            return this.parseRespData(resp, hideToast);
        });
    }

    postWithLoading(url: string, data: any, hideToast?: boolean) {
        Toast.loading({
            message: "加载中...",
            forbidClick: true,
            duration: 0
        });
        return this.caxios.post(url, data).then((resp: any) => {
            Toast.clear();
            return this.parseRespData(resp, hideToast);
        });
    }

    postList(url: string, data: any) {
        return this.caxios.post(url, data).then((resp: any) => {
            const { code, message } = resp;
            if (code === "00000") {
                return resp;
            } else {
                Toast.fail(message || "服务端异常");
                return false;
            }
        });
    }

    // 解析接口返回数据，异常处理
    parseRespData(resp: Result<any, any>, hideToast?: boolean) {
        const { code, data, message } = resp;
        if (code === "00000") {
            return data;
        } else {
            if (!hideToast) {
                Toast.fail(message || "服务端异常");
            }
            return false;
        }
    }
}

const request = Request.getInstance();

export default request;
